package com.ruoyi.cyb;


import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 验真银行卡信息的工具类
 * @author 26352
 * @Date 2023/3/21 11:02
 * @Description TODO
 * @Version 1.0
 */
public class AuthenticityUtils {

    public static boolean authenticitybankcard(String card_number,String name) {
        String host = "https://dfbank2.market.alicloudapi.com";
        String path = "/verify_name_bankcard";
        String method = "POST";
        String appcode = "3e21d7c6a74149069eb416913944bf1b";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("card_number", card_number);
        bodys.put("name", name);


        try {
            /**
             * HttpUtils
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             *
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());

            String s = EntityUtils.toString(response.getEntity());
            System.out.println(s+"--------------------------------");
            BankCardEntity bankCardEntity = JSON.parseObject(s, BankCardEntity.class);
            if (bankCardEntity.getState()==1){
                return true;
            }
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
